package com.raylu.util

import redis.clients.jedis.{Jedis, JedisPool, JedisPoolConfig}

import java.util.{Base64, Properties}

/**
 *
 * Description:
 *
 * Create by lucienoz on 2021/11/26.
 * Copyright © 2021 lucienoz. All rights reserved.
 */
object RedisUtil {
	private val properties : Properties = PropertiesUtil.load ( "config.properties" )
	private val jedisPool : JedisPool = createJedisPool()

	private def createJedisPool(): JedisPool ={
			val config : JedisPoolConfig = new JedisPoolConfig ()
			println(config.getClass)
			config.setMaxIdle ( properties.getProperty("redis.pool.setMaxIdle").toInt )
			println("test")
			config.setMinIdle ( properties.getProperty("redis.pool.setMinIdle").toInt )
			config.setMaxTotal ( properties.getProperty("redis.pool.setMaxTotal").toInt )
			config.setTestOnBorrow ( properties.getProperty("redis.pool.setTestOnBorrow").toBoolean )
			config.setBlockWhenExhausted ( properties.getProperty("redis.pool.setBlockWhenExhausted").toBoolean )
			config.setMaxWaitMillis ( properties.getProperty("redis.pool.setMaxWaitMillis").toInt )

		val jedisPool1 = new JedisPool ( config,
			properties.getProperty ( "redis.host" ),
			properties.getProperty ( "redis.port" ).toInt,
			2000,
			new String ( Base64.getDecoder.decode ( properties.getProperty ( "redis.passwd" ) ) ) )
			println(jedisPool1.getClass)
		jedisPool1

	}
	def getJedisFromPool() : Jedis = {
		jedisPool.getResource
	}

	def main(args : Array[String]) : Unit = {

		println ( new String(Base64.getDecoder.decode(properties.getProperty ( "redis.passwd" ))) )

		println ( "1".toInt )
		val jedis : Jedis = RedisUtil.getJedisFromPool ()
		println ( jedis.ping () )
		jedis.select(0)
		val str : String = jedis.get ( "user_info:id:10086" )
//		println ("str==>"+ str.getClass )


	}


}
